package org.sasha.roulette;

import javax.inject.Inject;

import org.sasha.roulette.actionbar.ActionBarView;
import org.sasha.roulette.chart.ChartView;
import org.sasha.roulette.data.Spin;
import org.sasha.roulette.dialog.SpinDialogView;
import org.sasha.roulette.feed.ColorFeedView;

import com.github.peholmst.mvp4vaadin.AbstractView;
import com.vaadin.event.ShortcutListener;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class RouletteViewImpl extends AbstractView<RouletteView, RoulettePresenter> implements RouletteView {

    private final ColorFeedView feedView;
    
    private final SpinDialogView dialog;
    
    private final ChartView chartView;
    
    private final ActionBarView actionBarView;
    
    private final HorizontalSplitPanel split = new HorizontalSplitPanel();
    
    private final VerticalLayout chartContainer = new VerticalLayout();
    
    private final Panel mainPanel = new Panel(split);
    
    private final ShortcutListener dialogShortCut = new ShortcutListener("KeyStroke", 13, new int[]{}) {
        @Override
        public void handleAction(Object sender, Object target) {
            showSpinDialog();
        }
    };
    
    @Inject
    public RouletteViewImpl(RoulettePresenter presenter, SpinDialogView dialog, ColorFeedView feedView, ChartView chartView, ActionBarView actionBar) {
        super(false);
        this.dialog = dialog;
        this.feedView = feedView;
        this.actionBarView = actionBar;
        this.chartView = chartView;
        setPresenter(presenter);
        presenter.setView(this);
        construct();
        init();
    }

    private void construct() {
        mainPanel.setSizeFull();
        mainPanel.addAction(dialogShortCut);
        
        split.setSplitPosition(90);
        split.setLocked(true);
        split.setSizeFull();
        
        chartContainer.setSizeFull();
        
        split.setFirstComponent(chartContainer);
        split.setSecondComponent(feedView.getViewComponent());
        
        chartContainer.addComponent(actionBarView.getViewComponent());
        chartContainer.addComponent(chartView.getViewComponent());
       
        chartContainer.setExpandRatio(chartView.getViewComponent(), 1f);
    }
    
    @Override
    public ComponentContainer getViewComponent() {
        return mainPanel;
    }

    @Override
    public void showSpinDialog() {
        dialog.show();
    }

    @Override
    public void onSpinRegistered(Spin spin) {
        refresh();
    }

    @Override
    public void refresh() {
        feedView.refresh();
        chartView.refresh();
    }
}
